<template>
  <div class="app-container">
    <el-form :inline="true" :model="form" class="demo-form-inline filter-container">
      <el-form-item label="会员账户名：">
        <el-input v-model="form.userName"></el-input>
      </el-form-item>

      <el-form-item>
        <el-col :span="12">
          <el-form-item prop="filterMin">
            <el-input v-model.number="form.filterMin">
              <template slot="prepend">
                <el-select v-model="form.filter" style="width: 120px;">
                  <el-option
                    v-for="item in consts.ORDER_FIELD"
                    :value="item.key"
                    :label="item.title"
                    :key="item.value"
                  ></el-option>
                </el-select>
              </template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="1" style="text-align: center">
          -
        </el-col>
        <el-col :span="6">
          <el-form-item prop="filterMax">
            <el-input v-model.number="form.filterMax"></el-input>
          </el-form-item>
        </el-col>
      </el-form-item>

      <el-button class="filter-item" type="primary" icon="el-icon-download" @click="exportHandle">导出</el-button>
      <el-button type="primary" v-waves icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
    </el-form>
    <el-table :data="tableData.list"
              v-loading="loading" element-loading-text="给我一点时间" border fit
              highlight-current-row
              style="width: 100%">
      <el-table-column label="会员账户名" align="center">
        <template slot-scope="{row}">
          {{row.userName}}
        </template>
      </el-table-column>

      <!--<el-table-column label="存款人数" align="center" prop="rechargeNum">-->
      <!--</el-table-column>-->
      <!--<el-table-column label="取款人数" align="center" prop="withdrawNum">-->
      <!--</el-table-column>-->
      <el-table-column label="存款金额" align="center" prop="rechargeSum" sortable>
        <template slot-scope="{row}">
          {{row.rechargeSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="取款金额" align="center" prop="withdrawSum" sortable>
        <template slot-scope="{row}">
          {{row.withdrawSum | currency}}
        </template>
      </el-table-column>

      <!--盈棋牌亏重新计算后的字段-->
      <el-table-column label="平台做庄总投注额" align="center" width="130" prop="betSum" sortable>
        <template slot-scope="{row}">{{row.betSum | currency}}</template>
      </el-table-column>
      <el-table-column label="平台做庄有效投注额" align="center" width="130" prop="betEffectSum" sortable>
        <template slot-scope="{row}" >
         <el-button type="text" @click="checkAgency('/report/report-game',row.userName)">{{row.betEffectSum | currency}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="平台做庄派彩额" align="center" width="130" prop="prizeSum" sortable>
        <template slot-scope="{row}">{{row.prizeSum | currency}}</template>
      </el-table-column>
       <el-table-column label="平台做庄公司输赢" align="center" width="130" prop="winSum" sortable>
        <template slot-scope="{row}">
          {{row.winSum | currency}}
        </template>
      </el-table-column>
       <el-table-column label="玩家对战投注额" align="center" width="130" prop="playerBetSum" sortable>
        <template slot-scope="{row}">{{row.playerBetSum | currency}}</template>
      </el-table-column>
      <el-table-column label="玩家对战玩家输赢" align="center" width="130" prop="playerWinSum" sortable>
        <template slot-scope="{row}">{{row.playerWinSum | currency}}</template>
      </el-table-column>
      <el-table-column label="玩家对战公司抽水" align="center" width="130" prop="playerWaterFee" sortable>
        <template slot-scope="{row}">{{row.playerWaterFee | currency}}</template>
      </el-table-column>
      <el-table-column label="公司总盈利" align="center" width="130" prop="totalWin" sortable>
        <template slot-scope="{row}">{{row.totalWin | currency}}</template>
      </el-table-column>
      <!--结束-->
       <!--新版本不需要的-->
      <!-- <el-table-column label="总投注额" align="center" prop="bettingTotalSum" sortable>
        <template slot-scope="{row}">
          {{row.bettingTotalSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="有效投注额" align="center" prop="bettingSum" sortable>
        <template slot-scope="{row}">
          <el-button type="text" @click="checkAgency('/report/report-game',row.userName)">{{row.bettingSum |
            currency}}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="派彩额" align="center" prop="prizeSum" sortable>
        <template slot-scope="{row}">
          {{row.prizeSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="公司输赢" align="center" prop="winSum" sortable>
        <template slot-scope="{row}">
          {{-1 * row.winSum | currency}}
        </template>
      </el-table-column> -->
      <el-table-column label="红利（包括加币-计入红利）" align="center" prop="bonusSum" sortable>
        <template slot-scope="{row}">
          <!--<el-button type="text" @click="checkAgency('/activity/activity-fund',row.userName)">{{row.bonusSum |-->
            <!--currency}}-->
          <!--</el-button>-->
          {{row.bonusSum | currency}}
        </template>
      </el-table-column>
      <el-table-column label="加币（不计红利）" align="center" width="150" prop="addCoinSum" sortable>
        <template slot-scope="{row}">{{row.addCoinSum | currency}}</template>
      </el-table-column>
      <el-table-column label="减币" align="center" width="150" prop="subCoinSum" sortable>
        <template slot-scope="{row}">{{row.subCoinSum | currency}}</template>
      </el-table-column>

      <el-table-column label="返水" align="center" prop="rebateSum" sortable>
        <template slot-scope="{row}">
          <!--<el-button type="text" @click="checkAgency('/activity-folder/activity-rebate',row.userName)">{{row.rebateSum |-->
            <!--currency}}-->
          <!--</el-button>-->
          {{row.rebateSum | currency}}
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { form, grid, dialog } from '@/build';

import {
  reportAgencyListApi as listApi, exportAgencyList,
} from '@/services/api/report';

export default {
  name: 'report-agency-sublist',

  mixins: [form, grid, dialog],

  props: ['rowData'],

  data() {
    const { uid } = this.rowData;
    return {
      tableData: {
        items: [],
        total: 0,
      },
      form: {
        userName: undefined,
        filterMin: undefined,
        filterMax: undefined,
        filter: undefined,
      },
      filters: {
        parentId: uid,
        page: 1,
        size: 20,
        stime: moment(this.rowData.time[0]).valueOf(),
        etime: moment(this.rowData.time[1]).valueOf() + 86399999,
      },
      filterDateType: false,
    };
  },
  methods: {
    submitFilterForm() {
      this.filters = { ...this.filters, ...this.form };
      this.getAll();
    },
    checkAgency(path, username) {
      this.$router.push({
        path,
        query: {
          username,
        },
      });
    },

    // 导出
    exportHandle() {
      const {
        stime, etime, page, size, parentId, userName, filterMin, filterMax, filter,
      } = this.filters;
      const ajaxData = {
        stime, etime, page, size, parentId, userName, filterMin, filterMax, filter,
      };
      const filename = '代理输赢明细报表.csv';
      const header = ['会员账户名', '存款金额', '取款金额', '平台做庄总投注额', '平台做庄有效投注额', '平台做庄派彩额', '平台做庄公司输赢', '玩家对战投注额', '玩家对战玩家输赢', '玩家对战公司抽水', '公司总盈利', '红利(包括加币-不计红利)','加币(不计红利)','减币', '返水'];
      const filterField = ['userName', 'rechargeSum', 'withdrawSum', 'betSum', 'betEffectSum', 'prizeSum', 'winSum', 'playerBetSum', 'playerWinSum', 'playerWaterFee', 'totalWin', 'bonusSum','addCoinSum', 'subCoinSum','rebateSum'];
      exportAgencyList(ajaxData).then(([result, data]) => {
        if (result) {
          const exportData = this.responseFormatter(data.list);
          this.export2Excel(header, filterField, exportData, filename);
        } else {
          this.$message({ message: `导出失败,${data.message}`, type: 'warning' });
        }
      });
    },
    // 格式化导出数据
    responseFormatter(data) {
      data.map((item) => {
        item.userName = _.currency(item.userName);
        item.rechargeSum = _.currency(item.rechargeSum);
        item.withdrawSum = _.currency(item.withdrawSum);
        item.betSum = _.currency(item.betSum);
        item.betEffectSum = _.currency(item.betEffectSum);
        item.prizeSum = _.currency(item.prizeSum);
        item.winSum = _.currency(item.winSum);
        item.playerBetSum = _.currency(item.playerBetSum);
        item.playerWinSum = _.currency(item.playerWinSum);
        item.playerWaterFee = _.currency(item.playerWaterFee);
        item.totalWin = _.currency(item.totalWin);
        item.bonusSum = _.currency(item.bonusSum);
        item.rebateSum = _.currency(item.rebateSum);
        item.addCoinSum=_.currency(item.addCoinSum);
        item.subCoinSum=_.currency(item.subCoinSum);
      });

      return data;
    },
  },

  mounted() {
    this.bindGet(listApi, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
    });

    this.getAll();
  },
};
</script>
